//http://www.nmc.gov.cn/publish/satellite/fy2.htm
//获取卫星云图图片
function formatTime(date, type) {
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let s = date.getMinutes();
    s = s < 10 ? ('0' + s) : s;
    if (type == 1) {
        return `${y}/${m}/${d}`
    }
    if (type == 2) {
        return `${y}/${m}/${d} ${h}:${s}`
    }
    if (type == 3) {
        return `${h}${s}`
    }
    return `${y}${m}${d}${h}${s}`;
};


/*
    t是图的类型 :
        卫星云图： WXCL/SEVP_NSMC_WXCL_ASC_E99_ACHN_LNO_PY
        雷达图：RDCP/medium/SEVP_AOC_RDCP_SLDAS_EBREF_ACCN_L88_PI
*/

function url(x, y, t) {
    if (t == 'WXCL/SEVP_NSMC_WXCL_ASC_E99_ACHN_LNO_PY')
        return `http://image.nmc.cn/product/${x}/${t}_${y}00000.JPG`;
    else
        return `http://image.nmc.cn/product/${x}/${t}_${y}00001.PNG`;
}

function satelliteImgFormat(st, et, type) {
    // let str = "2017/10/11 10:15";
    // str = str.replace(/-/g, "/");
    // let now = new Date(str);
    // let time = now.getTime() - 1000 * 60 * 60 * 8;
    // let end = formatTime(new Date(time));
    // let onlyDate = formatTime(new Date(time), true)
    // console.log(end, typeof end, onlyDate);
    // st = "2017/09/30 09:15"
    // et = "2017/09/30 09:15"
    let urlList = [];
    st = st.replace(/-/g, "/");
    et = et.replace(/-/g, "/");
    let _st = new Date(st).getTime() - 1000 * 60 * 60 * 8,
        _et = new Date(et).getTime() - 1000 * 60 * 60 * 8
    do {
        let item = new Date(_st);
        let num = formatTime(item, 3);
        console.log(num)
        if (!(num > "1545")) { //过滤23:45-------8:15
            urlList.push(url(formatTime(item, 1), formatTime(item), type));
        }
        if (type == "WXCL/SEVP_NSMC_WXCL_ASC_E99_ACHN_LNO_PY")
            _st += 1000 * 60 * 30;
        else
            _st += 1000 * 60 * 6;
    } while (_st <= _et)
    console.log(urlList);
    return urlList;
}

function timeList(type) {
    let now = new Date(),
        bt, list = [];
    let m = now.getMinutes();
    debugger;

    if (type == "WXCL/SEVP_NSMC_WXCL_ASC_E99_ACHN_LNO_PY") {
        if (m >= 55) {
            now.setMinutes(45);
        } else if (m <= 25) {
            now.setMinutes(15);
            now = new Date(now.getTime() - 1000 * 60 * 30);
        } else {
            now.setMinutes(15);
        }
        bt = now.getTime();
        for (let i = 0; i < 12; i++) {
            let item = new Date(bt - 1000 * 60 * 30 * i);
            let num = formatTime(item, 3);
            if (!(num > "2345" || num < "0815")) { //过滤23:45-------8:15
                list.push(formatTime(item, 2));
            }
        }
    } else {
        now.setMinutes(m - m % 6 - 6)
        bt = now.getTime();

        for (let i = 0; i < 12; i++) {
            let item = new Date(bt - 1000 * 60 * 6 * i);
            let num = formatTime(item, 3);
            if (!(num > "2300" || num < "0800")) { //过滤23:45-------8:15
                list.push(formatTime(item, 2));
            }
        }
    }
    return list
}


export {
    satelliteImgFormat,
    timeList
}